import markdown
from django.shortcuts import render,get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.postgres.search import SearchVector
from .models import Log,Category,Tag

def global_variable(request):
    categories = Category.objects.all()
    tags = Tag.objects.all()
    archive_list = Log.objects.archive()
    return locals()

def index(request):
    latestlogs = Log.objects.all().order_by('-date','-time')[:10]

    return render(request,
                'diary/index.html',
                locals())

def list(request,lid):
    loglist = Log.objects.filter(category_id=lid).order_by('-date','-time')
    categoryoflog = Category.objects.filter(id=lid)[0]

    page = request.GET.get('page')
    print(type(page))
    paginator = Paginator(loglist, 5)
    try:
        loglist = paginator.page(page)
    except PageNotAnInteger:
        loglist = paginator.page(1)
    except EmptyPage:
        loglist = paginator.page(paginator.num_pages)
    return render(request,
            'diary/list.html',
            locals())

def show(request,sid):
    log = get_object_or_404(Log,pk=sid)
    # markdown.extensions.extra：用于标题、表格、引用这些基本转换
    # markdown.extensions.codehilite：用于语法高亮
    # markdown.extensions.toc：用于生成目录
    log.body = markdown.markdown(log.body,
            extensions=[
                'markdown.extensions.extra',
                'markdown.extensions.codehilite',
                'markdown.extensions.toc',
                ])
    previous_logs = Log.objects.filter(date__lt=log.date,category=log.category)
    if previous_logs:
        previous_log = previous_logs[0]
    next_logs = Log.objects.filter(date__gt=log.date,category=log.category)
    if next_logs:
        next_log = next_logs.last()
    return render(request,
                'diary/show.html',
                locals())

def tag(request,tag):
    tag = Tag.objects.filter(name=tag)[0]
    loglist = Log.objects.filter(tags__name=tag)

    page = request.GET.get('page')
    paginator = Paginator(loglist, 5)
    try:
        loglist = paginator.page(page)
    except PageNotAnInteger:
        loglist = paginator.page(1)
    except EmptyPage:
        loglist = paginator.page(paginator.num_pages)
    return render(request,
            'diary/tag.html',
            locals())

def archive(request,year,month):
    archive = "%s年%s月" % (year,month)
    loglist = Log.objects.filter(date__year=year,date__month=month)

    page = request.GET.get('page')
    paginator = Paginator(loglist, 5)
    try:
        loglist = paginator.page(page)
    except PageNotAnInteger:
        loglist = paginator.page(1)
    except EmptyPage:
        loglist = paginator.page(paginator.num_pages)
    return render(request,
            'diary/archive.html',
            locals())
    

def search(request):
    query = request.GET.get('search')
    loglist = Log.objects.annotate(
            search=SearchVector('title','body'),
            ).filter(search=query)
    loglist = Log.objects.filter(body__contains=query)
    print(loglist)
    page = request.GET.get('page')
    paginator = Paginator(loglist, 5)
    try:
        loglist = paginator.page(page)
    except PageNotAnInteger:
        loglist = paginator.page(1)
    except EmptyPage:
        loglist = paginator.page(paginator.num_pages)
    return render(request,
            'diary/search.html',
            locals())

def about(request):
    return render(request,
            'diary/about.html',
            locals())
